package game.tileMap.model
{
	
	import game.tileMap.DataBase;
	import game.tileMap.model.vo.ActionResult;
	import game.tileMap.model.vo.Skill;
	import game.tileMap.model.vo.Unit;

	
	public class Battler extends Unit
	{
		
		public var battler_name             
		public var battler_hue              
		public var index:int
		
		private var _hp:int                       
		private var _mp:int                   
		 
		  
		  public var hidden :Boolean;
		  // 不死身标志
		  public var immortal:Boolean ;                
		  public var animation_id ;           //动画 ID
		  // 动画纵向翻转标志 // 白色屏幕闪烁标志 //闪烁标志//倒下标志
		  public var animation_mirror :Boolean,white_flash:Boolean,blink :Boolean                 
		  public var collapse:Boolean;          
		         //行动结果：跳过标志 落空标志 闪躲标志 会心一击标志 吸收标志 体力伤害标志 魔力伤害标志
		
		  //行动结果
		  public var actResult:ActionResult;
		  public var action : BattleAction;    
		  
		  
		  
		  
		  protected var skillIdList:Array;
		  private var state_turns:Object = {}  
					
					
		public var 	maxhp_plus :Number,maxmp_plus:Number,
					atk_plus :Number,def_plus:Number,spi_plus:Number,agi_plus:Number;   
		  
		
					
		//状态ID数组			
		public var stateIdList:Array;  
		//已存在状态ID
		public var remainedStateId_arr:Array;  
		//新添加的状态ID
		public var addedStateId_arr:Array; 
		//移除的状态ID
		public var removedStateId_arr:Array
		
		protected var db:DataBase=DataBase.getInstance();
		
		[Bindable]
		public function Battler(){
			init();
		
			
		}
		
		protected function init():void{
			battler_name = ""
			stateIdList=[]
			
			immortal = false;
			
			
			animation_id = 0;
			//	    skinId=1;
			//	    animation_hit = false;
			
			actResult=new ActionResult();
			action = new BattleAction(this);
			
			clear_extra_values();
			clear_sprite_effects();
			clear_action_results();
		}
		
		//删除数组中与参数相同的ID项

	

		

		private function deleteIdFromArray(arr:Array,id:int):void{
			var index:int=arr.indexOf(id);
			if (index>0){
				arr.splice(index,1);
			}	
		}
	
		
	
	//判断是否为玩家操控的角色	
	/*	public function get isActor():Boolean{
			if (friendTroop is Party){
				return true;
			}
			return false;
		}*/
		
//========================基本数值=vo值+装备值=====================================================================
		
		
		public function get base_maxhp():int{
			return 0;
		}
		
		public function get base_maxmp():int{
			return 0;
		}
		
		public function get base_atk():int{
			return 0;
		}
		
		public function get base_def():int{
			return 0;
		}
		
		public function get base_spi():int{
			return 0;
		}
		
		public function get base_agi():int{
			return 0;
		}
		
		public function get hit():int{
			return 0;
		}
		
		public function get eva():int{
			return 0;
		}
		
		//获取会心一击率
		public function get cri():int{
			return 0;
		}
		//获取受击率
		public function get odds():int{
			return 0;
		}
		
//==================真实数值=========================================
		public function get maxhp():int{
			return base_maxhp+maxhp_plus;
		}
		
		public function get maxmp():int{
			return base_maxmp;
		}
		
		public function get atk():int{
			var n:int= Math.min(base_atk + atk_plus, 999);
			return n;
		}
		
		public function get def():int{
			var n:int= Math.min(base_def + def_plus, 999);
			return n;
		}
		
		public function get spi():int{
			var n:int= Math.min(base_spi + spi_plus, 999);
			return n;
		}
		
		public function get agi():int{
			var n:int= Math.min(base_agi + agi_plus, 999);
			return n;
		}
	
		//-----------------------------------------------
		public function set maxhp(value:int):void{
			maxhp_plus += value - maxhp
		//	maxhp_plus = Math.min(maxhp_plus, 9999);
			_hp = Math.min(_hp, maxhp);
		}
		
		public function set maxmp(value:int):void{
		}
		
		public function set atk(value:int):void{
			atk_plus+=value-atk;
		}
		
		public function set def(value:int):void{
		}
		
		public function set spi(value:int):void{
		}
		
		public function set agi(value:int):void{
		}
		
		
		
//=====================================================================	
		[Bindable]
		public function get hp():Number{
			return _hp
		}
		/**
		 * 更改HP，如果为0,附加上死亡状态
		 * 
		 */
		public function set hp(newhp:Number):void{
			newhp=Math.min(newhp, maxhp);
			_hp = Math.max(newhp, 0);
			if (_hp == 0 && !hasState(1) &&!immortal){
				add_state(1)                
				addedStateId_arr.push(1)
				
			}else if (_hp > 0 && hasState(1)){
				remove_state(1)            
				removedStateId_arr.push(1)
			}
		}
		
		[Bindable]
		public function get mp():Number{
			return _mp
		}
		
		public function set mp(newmp:Number):void{
			newmp=Math.min(newmp, maxmp);
			_mp = Math.max(newmp, 0);
			
		}
		
		public function recover_all():void{
			_hp = maxhp
			_mp = maxmp
//			for i in @states.clone do  {
//				remove_state(i)
//			}
				
		}
		
		
		/**
		 *可以使用技能的判定 
		 * @param skill
		 * @return 
		 * 
		 */		
		public function canUseSkill(skill:Skill):Boolean{
			//	return false unless skill.is_a?(RPG::Skill)
    		
			
			if (!movable) return false;
		
			
    		return true;
    	}
		
		public function get skills():Array{
			var result:Array=[];
			
		
			return result;
		}
		
		public function get atk_animation_id():String{
			//two_swords_style
//			if (true){
//				if (weapons[0]!= null){
//					return weapons[0].animation_id 
//				}
//			}
			
			
			return "1";
			
		}
		
		
		public function get friendTroop():Troop{
			if (camp==0)return map.troops[0];
			return map.troops[1]
		}
		public function get opponentsTroop():Troop{
			if (camp==0)return map.troops[1];
			return map.troops[0]
		}
		
		
		
		
		
//------------------------获取状态列表数组------------------------------------	
		// 获得状态数组
		public function get states():Array{
			var  result:Array = [];
			
			   
    		return result;
		}
		
		
	
//		
		//获取先前行动所附加的状态
		public function get added_states():Array{
			var  result:Array = [];
			
			
			
    		return result;
		}
		
		//获取先前行动所移除的状态
		public function get removed_states():Array{
			var  result:Array = [];
			
    		return result;
		}
		
		//获取先前行动後所剩馀的状态  ，已用过的，例如：尝试把以睡着的角色附加睡眠状态
		public function get remained_states():Array{
			var  result:Array = [];
			
    		return result;
		}
		
		//判断先前行动是否对状态有效果
		public function get statesActive():Boolean{
			if (actResult.added_states.length>0||
				actResult.removed_states.length>0||
				actResult.remained_states.length>0){
				return true
			}
    		return false
		}
   
		
//-----------------------------------------------------------------------------------------------
		//战斗不能判定
		public function get isDead():Boolean{
				return (!hidden && _hp == 0 && !immortal)
		}
		
		//存在判定
		public function get isExist():Boolean{
			return (!hidden && !isDead);
		}

	

		//判断状态 [减少命中率]
		public function get isReduceHitRatio():Boolean{
			
   			 return false;
		}
		
		//判断状态 [连续伤害]
		public function get isSlipDamage():Boolean{
			
   			 return false;
		}
		
		//是否具有自动回复HP能力
		public function get isAutoHpRecover():Boolean{	
			return false;
		}
		
		
		/**
		 *获取限    从当前附加的状态中获取最大的限制
		 * @return 限制行动（0：无、1：不能使用魔法、2：普通攻击敌人、3：普通攻击同伴、4：不能行动、5：无法行动或回避。） 
		 * 
		 */		
		public function get restriction():int{
			var restriction_max:int = 0
			
			
			return restriction_max
		}
   
   
   
//----------------------状态判定-------------------------------------------	
		
		
		//可以输入命令判定
		public function get inputable():Boolean{
			return (!hidden && restriction <= 1);
		}
		
		//可以行动判定
		public function get movable():Boolean{
			return (!hidden && restriction < 4);
		}
		
		//回避可能判定
		public function get parriable():Boolean{
			return (!hidden && restriction < 5);
		}
		
		//沈默状态判断
		public function get isSilent():Boolean{
			return (!hidden && restriction == 1);
		}
		
		//暴走状态判定
		public function get isBerserker():Boolean{
			return (!hidden && restriction == 2);
		}
		
		//丧乱状态判定
		public function get isConfusion():Boolean{
			return (!hidden && restriction == 3);
		}
		
		//防御中判定
		public function get isGuarding():Boolean{
			return false
		}
		
		//获取攻击属性id集合（一个武器可包含多种攻击属性）
		public function get element_set():Array{
			return [];
		}
		//获取属性修正值
		public function getElementRate(element_id:int):int{
			
			return 100;
		}
		
		
		
		//是否已含有该状态
		public function hasState(state_id:int):Boolean{
			if (stateIdList.indexOf(state_id)!=-1){
				return true;
			}
			return false;
		}
   
		/**
		 * 判断状态是否已满
		 * @param state_id 状态ID
		 * @return  如该状态的剩余回合数等于该状态自动移除的回合数则返回true
		 * 
		 */		
		public function isStateFull(state_id:int):Boolean{
			
			return false;
		}
		
		public function state_probability(state_id:int):int{
			 return 0
		}
   		
		
		
		
		
	
		
//--------------------------------------------------------------------------
		
		
	
		
		/**
		 * 清除能力值变量
		 */
		public function clear_extra_values():void{
			maxhp_plus = 0;
		    maxmp_plus = 0;
		    atk_plus = 0;
		    def_plus = 0;
		    spi_plus = 0;
		    agi_plus = 0;
		}
		
		public function clear_sprite_effects():void{
			animation_id = 0;
		    animation_mirror = false;
		    white_flash = false;
		    blink = false;
		    collapse = false;
		}
		
		
		//清空行动结果，初始化时应调用一次
		public function clear_action_results():void{
			actResult.clear();
		    //剩馀状态（ID数组）	 
			addedStateId_arr=[] 
		     removedStateId_arr=[]
			remainedStateId_arr =[]
		         	
		}
		
		
		
		public function makeAction():void {
			//action.setAttack();
			
			//action.decideRandomTarget(friendTroop,opponentTroop)
		}
		
		/**
		 *附加状态 
		 * @param state_id 状态 ID
		 * 
		 */		
		public function add_state(state_id:int):void{
			
		}
		
		public function remove_state(state_id:int):void{
			if (!hasState(state_id)){
				return;
			}
		    if (state_id == 1 &&_hp == 0 )  {    
		    	 _hp = 1                           
		     }
		    stateIdList.splice(stateIdList.indexOf(state_id),1);             
		    delete state_turns[state_id]; 
		     
		}
		/**
		 * 状态自然解除 (回合改变时调用)
		 */		
		
		public function remove_states_auto():void{
			
				
			
		}
		
		/**
		 *状态攻击解除 (受到物理伤害时调用) 
		 * 
		 */		
		public function remove_states_shock():void{
			
		}
		
		
		
		
		//执行倒下
		public function performCollapse():void{
			if (isDead){
				
				collapse = true
				trace("倒下"+collapse)
				//Sound.play_enemy_collapse
			}
		
		}
		
		
		
		/**
		 * #--------------------------------------------------------------------------
			# ● 计算分散度
			#     damage   : 伤害
		#     variance : 分散度
		 */
		public function apply_variance(damage:Number, variance:int):int{
			if (damage != 0){
			//	amp = [damage.abs * variance / 100, 0].max    # 计算极差
			//	damage += rand(amp+1) + rand(amp+1) - amp     # 执行分散度
			}                              
			return damage
		}
		
		
		public function slip_damage_effect():void{
		
			if (isSlipDamage && _hp > 0){
				actResult.hp_damage = apply_variance(maxhp / 10, 10)
				
					 if (actResult.hp_damage >= _hp){
						 actResult.hp_damage =_hp - 1
					 }
				hp -= actResult.hp_damage
				trace("连续伤害效果"+actResult.hp_damage)
			}
				
		}
		
		public function apply_guard(damage:Number):Number
		{
				if (damage > 0 &&isGuarding) {
					damage /= 2      
				}
				return damage
				
		}
		
		public function canAttack(unit:Unit):Boolean
		{
			
			if (Math.abs(unit.x-x)<=atkR&&Math.abs(unit.y-y)<=atkR){
				return true;
			}
			return false;
		}
	}
}